package net;

import net.chemistry.Atom;
import net.chemistry.AtomID;
import net.chemistry.Molecule;
import net.game.GUIComponent;
import net.game.MenuBarGUI;
import net.game.MoleculeBuilderGUI;
import net.util.ArrayList2;
import net.util.MouseUtil;
import net.util.ThrowableHandler;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;


public class Molecules 
{
	public static final int WIDTH = 1000, HEIGHT = 600;
	
	private ThrowableHandler throwablehandler = new ThrowableHandler();

	private ArrayList2<GUIComponent> gui = new ArrayList2<>(); 
	
	public static void main(String [] args)
	{
		Molecules molecules = new Molecules();
		molecules.init();
	}
	
	private static void initGl()
	{
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glClearColor(0.1f,0.3f,0.2f,0);
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		GL11.glViewport(0,0,WIDTH,HEIGHT);

		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0,WIDTH,HEIGHT,0,1,-1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
	}

	public static void cleanUp()
	{
		Mouse.destroy();
		Keyboard.destroy();
		Display.destroy();
	}

	public void init()
	{	
		try 
		{
			Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
			Display.create();
			Mouse.create();
			Keyboard.create();
			Display.setVSyncEnabled(true);
			Display.setResizable(false);
			Display.setTitle("Molecular Anatomy");
			
			initGl();

			//System.out.close();
			
			gui.add(new MoleculeBuilderGUI());
			gui.add(new MenuBarGUI());
			
			mainLoop();
		} 
		catch (LWJGLException e) 
		{
			System.err.println("Exception creating screen: " + e.getMessage());
		}
		finally
		{
			cleanUp();
		}
	}

	public void mainLoop()
	{	
		Atom root0 = new Atom(AtomID.CARBON);
		Atom root1 = new Atom(AtomID.CARBON);
		
		Molecule ethane = new Molecule(root0);
		
		try
		{
			ethane.appendAtom(root0, new Atom(AtomID.HYDROGEN), 1, false);
			ethane.appendAtom(root0, new Atom(AtomID.HYDROGEN), 1, false);
			ethane.appendAtom(root0, new Atom(AtomID.HYDROGEN), 1, false);
			ethane.appendAtom(root0, root1, 1, false);
			ethane.appendAtom(root1, new Atom(AtomID.HYDROGEN), 1, false);
			ethane.appendAtom(root1, new Atom(AtomID.HYDROGEN), 1, false);
			ethane.appendAtom(root1, new Atom(AtomID.HYDROGEN), 1, false);
		}
		catch (ChemistryException e)
		{
			e.printStackTrace();
		}
		
		int i = 0;
		
		long time;
		
		while(!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
		{
			time = System.nanoTime();
			
			try
			{
				GL11.glClear(GL11.GL_COLOR_BUFFER_BIT  | GL11.GL_DEPTH_BUFFER_BIT);
				
				GL11.glColor4f(1,1,1,1);
				
				ethane.render(300,400,i);
	
				for(GUIComponent guicomp : gui)
				{
					guicomp.render();
				}
				
				Display.update();
				
				MouseUtil.update();
				
				time = System.nanoTime() - time;
				
				//System.out.println(time);
				
				Display.sync(30);
			}
			catch(Exception e)
			{
				System.out.println(ThrowableHandler.stackTrace(e));
				throwablehandler.handleException(e);
			}
			catch(Error e)
			{
				throwablehandler.handleErrorAndExit(e);
			}
		}
	}
}


